//
//  QQLogin.swift
//  DollBuy
//
//  Created by 郑东喜 on 2016/11/13.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

class QQLogin: NSObject {
    static let shared = QQLogin()
    
    /**
     ## qq第三方登陆功能
     实现QQ第三方登陆监听流程
     */
    func qqObserver() -> Void {
        //通知名称常量
        let qqNotifyChatMsgRecv = NSNotification.Name(rawValue:"qqdelegate")
        
        //接受通知监听
        NotificationCenter.default.addObserver(self, selector:#selector(qqdidMsgRecv(notification:)),name: qqNotifyChatMsgRecv, object: nil)
        
    }
    
    //QQ通知处理函数
    func qqdidMsgRecv(notification:NSNotification){
        print("QQ传过来的授权数据")
        print("didMsgRecv: \(notification.userInfo)")
        
        guard let dic = notification.userInfo as? [String : Any] else {
            return
        }
        
        let qqLocalSaveData = dic as NSDictionary
        
        if qqLocalSaveData.count == 0 {
            return
        }
        
        print(qqLocalSaveData["qqOPENID"] as Any,qqLocalSaveData["qqHEADIMGURL"] as Any,qqLocalSaveData["qqNICKNAME"] as Any)
        
        ///获取QQopen id
        let qqopenID = qqLocalSaveData["qqOPENID"] as! String
        
        ///获取QQ头像地址
        let qqHeadImgURL = qqLocalSaveData["qqHEADIMGURL"] as! String
        
        ///QQ昵称
        let qqNickName = qqLocalSaveData["qqNICKNAME"] as! String
        
        print("QQopenID",qqopenID)
        print("QQ头像地址",qqHeadImgURL)
        print("QQ昵称",qqNickName)
        
        
        let param = ["deviceid" : deviceID,
                     "devtype" : deviceTpye,
                     "openid" : qqopenID,
                     "platform" : 0,
                     "headimg" : qqHeadImgURL,
                     "nickname" : qqNickName,] as [String :Any]
        
        print("qqlogin---")
        
        postWithPath(path: oauthURL, paras: param, success: { (response) in
            
            print("qqlogin---")
            
            guard let dic = response as? NSDictionary else {
                return
            }
            
            print(dic)
            
            //提取提示语
            let alertmsg = dic["resultmsg"] as! String
            
            
            if alertmsg == "授权成功" {
                
                
                MBManager.showBriefAlert("授权登陆成功")
                
                print("------")
                
//                UIApplication.shared.keyWindow?.rootViewController?.navigationController?.pushViewController(MeViewController(), animated: true)
                
                //取出登陆后的token
                guard let tokenData = dic["data"] as? NSDictionary else {
                    return
                }
                
                let token = tokenData["token"] as! String
                
                print("token",token)
                
                //将token存入本地
                localSave.set(token, forKey: userToken)
                localSave.synchronize()
                
                //获取登陆信息
                print((dic["data"] as! NSDictionary)["user"] as! NSDictionary)
                guard let list = (dic["data"] as? NSDictionary)?["user"] as? NSDictionary else {
                    return
                }
                
                //list数组为空，就返回
                if list.count == 0 {
                    return
                }
                
                
                let qqImg = list["img"] as! String
                let qqNickName = list["username"] as! String
                
                print(qqImg,qqNickName)
                
                
                //发送通知
                //通知名称常量
                let notifyChatMsgRecv = NSNotification.Name(rawValue:qqNotifi)
                NotificationCenter.default.post(name:notifyChatMsgRecv, object: nil, userInfo: ["qqImgUrl" : qqImg,
                                                                                                "qqNickName": qqNickName] as [String : Any])
                
                
            }
            
        }, failure: { (error) in
            print(error.localizedDescription)
        })
    }
}


